<?php
/**
 * Hanles Currencie conversions and price display.
 * @copyright 2008 Nicolas Schudel
 * @copyright Portions Copyright 2003-2006 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
 */

class Currencies {
	private $db;
	private $cf;
	private $default_currency;
	public $currencies;

	function __construct() {
		$this->db = ServiceLocator::getService( 'ConnectionFactory' );
		$this->cf = ServiceLocator::getService( 'Configuration' );
		$this->db->connect($this->cf->get('DB_SERVER'), $this->cf->get('DB_SERVER_USERNAME'), $this->cf->get('DB_SERVER_PASSWORD'), $this->cf->get('DB_DATABASE'), $this->cf->get('DB_USE_PCONNECT'), false);
		$this->default_currency = $this->getDefaultCurrency();
		$this->currencies = $this->createCurrenciesArray();
	}
	
	private function createCurrenciesArray() {
		$holder = array();
		
		$currencies_query = "select code, title, symbol_left, symbol_right, decimal_point,
							thousands_point, decimal_places, value
							from " . $this->cf->get('TABLE_CURRENCIES');
		
		$currenciesTable = $this->db->execute($currencies_query);
		
		while (!$currenciesTable->EOF) {
			$holder[$currenciesTable->fields['code']] = array('title' => $currenciesTable->fields['title'],
			'symbol_left' => $currenciesTable->fields['symbol_left'],
			'symbol_right' => $currenciesTable->fields['symbol_right'],
			'decimal_point' => $currenciesTable->fields['decimal_point'],
			'thousands_point' => $currenciesTable->fields['thousands_point'],
			'decimal_places' => $currenciesTable->fields['decimal_places'],
			'value' => $currenciesTable->fields['value']);
			
			$currenciesTable->moveNext();
		}
		
		return $holder;
	}
	
	public function currencyExists($currency) {
		$sql = " SELECT code FROM " . $this->cf->get('TABLE_CURRENCIES') . " WHERE code LIKE '" . $currency . "' LIMIT 0 , 30 ";
		$currency = $this->db->execute($sql);
		if (isset($currency->fields) && $currency->fields > 0) return true;
		else return false;
	}
	
	public function getDefaultCurrency() {
		$default_currency_query = 'SELECT cur.code FROM ' . $this->cf->get('TABLE_CURRENCIES') . ' cur WHERE value = 1 LIMIT 0, 30 ';
		$default_currency = $this->db->execute($default_currency_query);
		return $default_currency->fields['code'];
	}
	
	private function notNull($value) {
		if (is_array($value)) {
			if (sizeof($value) > 0) {
				return true;
			} else {
				return false;
			}
		} else {
			if (($value != '') && (strtolower($value) != 'null') && (strlen(trim($value)) > 0)) {
				return true;
			} else {
				return false;
			}
		}
	}

	function format($number, $calculate_currency_value = true, $currency_type = '', $currency_value = '') {
		
		if (empty($currency_type)) $currency_type = $this->default_currency;
		
		if ($calculate_currency_value == true) {
			$rate = ($this->notNull($currency_value)) ? $currency_value : $this->currencies[$currency_type]['value'];
			$format_string = $this->currencies[$currency_type]['symbol_left'] . number_format(round($number * $rate, $this->currencies[$currency_type]['decimal_places']), $this->currencies[$currency_type]['decimal_places'], $this->currencies[$currency_type]['decimal_point'], $this->currencies[$currency_type]['thousands_point']) . $this->currencies[$currency_type]['symbol_right'];
		} else {
			$format_string = $this->currencies[$currency_type]['symbol_left'] . number_format(round($number, $this->currencies[$currency_type]['decimal_places']), $this->currencies[$currency_type]['decimal_places'], $this->currencies[$currency_type]['decimal_point'], $this->currencies[$currency_type]['thousands_point']) . $this->currencies[$currency_type]['symbol_right'];
		}
		
		return $format_string;
	}
	
	function value($number, $calculate_currency_value = true, $currency_type = '', $currency_value = '') {
		
		if (empty($currency_type)) $currency_type = $this->default_currency;
		
		if ($calculate_currency_value == true) {
			if ($currency_type == DEFAULT_CURRENCY) {
			$rate = ($this->notNull($currency_value)) ? $currency_value : 1/$this->currencies[$_SESSION['currency']]['value'];
		} else {
			$rate = ($this->notNull($currency_value)) ? $currency_value : $this->currencies[$currency_type]['value'];
		}
			$currency_value = round($number * $rate, $this->currencies[$currency_type]['decimal_places']);
		} else {
			$currency_value = round($number, $this->currencies[$currency_type]['decimal_places']);
		}
		
		return $currency_value;
	}
	
	function is_set($code) {
		if (isset($this->currencies[$code]) && $this->notNull($this->currencies[$code])) {
			return true;
		} else {
			return false;
		}
	}
	
	function get_value($code) {
		return $this->currencies[$code]['value'];
	}
	
	function get_decimal_places($code) {
		return $this->currencies[$code]['decimal_places'];
	}
	
	function display_price($products_price, $products_tax, $quantity = 1) {
		return $this->format(zen_add_tax($products_price, $products_tax) * $quantity);
	}
}
?>